<?php
/**
 * 生成二维码交易(客户扫商户生成的二维码付款)
 */
include '../Util/phpqrcode.php';
include_once '../Util/Common.php';
require_once __DIR__ . './../Util/Utils.php';

use UtilMethod\Utils;
$utils = new Utils();

$post_data = [
    'account' => ACCOUNT,
    'orderCode' => 'tb_WeixinPay'
];

$msgDate = [
    'channel_code' => 'ALIPAY',
    'amount' => 1,
    'info' => ''
];

$post_data['msg'] = base64_encode(json_encode($msgDate, JSON_UNESCAPED_SLASHES));

$priKeyFilePath = PRIVATEKEYPATH . $post_data['account'] . "_private_key.pem";
$sign = $utils::rsaDataSign($post_data['msg'], $priKeyFilePath); //RSA签名


$data = base64_encode(json_encode($post_data, JSON_UNESCAPED_SLASHES));
$decrypt = $utils::rsaPublicEncrypt($data, PUBLICKEYPATH); //RSA公钥加密

$send_data = [
    'data' => $decrypt,
    'signature' => $sign
];

$res = $utils::send_post1(KUBEI, json_encode($send_data, JSON_UNESCAPED_SLASHES));
$res = json_decode($res, true);
$res_data = $res['data'];
$res_sign = $res['signature'];

$original = $utils::rsaPrivateDecrypt(base64_decode($res_data), $priKeyFilePath); //RSA私钥解密
$original = json_decode($original, true);
$res_msg = json_decode($original['msg'], true);

//验证签名
$valid = $utils::isValid(json_encode($res_msg, JSON_UNESCAPED_SLASHES), base64_decode($res_sign), PUBLICKEYPATH);

if ("000000" === $res_msg['respCode']) {
    print("申请二维码成功.</br>二维码地址:" . $res_msg['QRcodeURL'] . "</br>订单号:" . $res_msg['orderId']);
    QRcode::png($res_msg['QRcodeURL'],'qrcode.png');//生成二维码
    echo '</br><img src="qrcode.png">';//输出二维码图片
} else {
    print("申请二维码失败</br>返回码:" . $res_msg['respCode'] . "</br>失败原因:" . $res_msg['respInfo'] . "<br/>");
    var_dump($res_msg);
}
